package com.zhumo.sz.monitor.feishu;

import com.alibaba.fastjson.JSONObject;
import com.zhumo.sz.common.http.HttpClientHelper;
import com.zhumo.sz.entity.FeiShuMessage;
import com.zhumo.sz.enums.FeiShuMessageCategory;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.apache.http.entity.ContentType;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

/**
 * @author zhumo
 * @comment 飞书机器人监控 用于监控集群的运行状态和任务脚本的执行情况,并向飞书发送消息
 * @time 2024-02-22
 */

@Slf4j
public class FeiShuRobotMonitor {

    /**
     * 项目飞书机器人的WebHookUrl
     */
    private static final String WebHookUrl = "https://open.feishu.cn/open-apis/bot/v2/hook/81592142-4415-4d17-b5ad-e57b18e4c944";

    /**
     * 项目发送Http请求所采用的请求格式
     */
    private static final String ContentTypeJsonHeader = "application/json;charset=UTF-8";

    //告警发生时间,消息发送的实际时间飞书自带
    public static FeiShuMessage sendMessage(String message, int categoryId,String project, String time, HttpClient httpClient) throws IOException {
        HashMap<String, Object> json = new HashMap<>();
        HashMap<String, Object> text= new HashMap<>();
        json.put("msg_type", "text");
        String category = "";
        switch (categoryId){
            case 0:
                category = FeiShuMessageCategory.COMMON.getCategory();
                break;
            case 1:
                category = FeiShuMessageCategory.WARN.getCategory();
                break;
            case 2:
                category = FeiShuMessageCategory.ERROR.getCategory();
                break;
        }
        text.put("text", "项目名称: " + project + " 消息级别: " + category + " 内容: " + message + " Time: " + time);
        json.put("content", text);
        //封装好的http工具
        FeiShuMessage feiShuMessage = new FeiShuMessage();
        if (HttpClientHelper.checkClient(httpClient)){
            JSONObject response = HttpClientHelper.sendJsonPostRequest(WebHookUrl, ContentTypeJsonHeader, json, ContentType.APPLICATION_JSON, httpClient);
            //如果飞书发送失败,则不进行存储,发送成功或者发送中
            if (!Objects.isNull(response)){
                int statusCode = Integer.parseInt(response.getString("StatusCode"));
                feiShuMessage.setStatus(statusCode);
                if (statusCode == 0 || statusCode == 1){
                    String data = response.getString("data");
                    String statusMessage = response.getString("StatusMessage");
                    feiShuMessage.setMessage(message);
                    feiShuMessage.setStatusMessage(statusMessage);
                    feiShuMessage.setData(data);
                    feiShuMessage.setCategoryId(categoryId);
                    feiShuMessage.setEventTime(time);
                }
                else if (statusCode == 2){
                    log.error("飞书WebHook消息请求异常");
                    feiShuMessage.setStatusMessage("request error");
                }
                else {
                    throw new RuntimeException("The return result of Feishu is abnormal");
                }
            }
        }
        return feiShuMessage;
    }
}
